fn main() {
    let mut s = String::from("hello world");

    let word = first_word_byslice(&s);
    s.clear();

    println!("{word}");
}

fn calculate_length(s: String) -> usize {
    let length = s.len(); // len() 返回字符串的长度

    length
}

fn calculate_length_with_reference(s: &String) ->usize {
    let length = s.len();
    length
}

fn change(some_string: &mut String) {
    borrow_agin(some_string);
    some_string.push_str(", world");
    println!("{}", calculate_length_with_reference(some_string));
}

fn borrow_agin(some_string: &mut String) {
    some_string.push_str(", world");
}
fn first_word(s: &String) -> usize {
    let bytes = s.as_bytes();

    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return i;
        }
    }

    s.len()
}

fn first_word_byslice(s: &String) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[0..i];
        }
    }
    return &s[..];
}